package com.yanqu.road.server.cmd.activity.suoyao;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.pb.activity.SuoYaoActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.suoyao.SuoYaoModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.suoyao.SuoYaoMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_SUOYAO_ACTIVITY_DRAW_BOX, desc = "锁妖塔抽盲盒")
public class SuoYaoDrawBoxCmd extends AbstractCommand {

    private int drawIndex;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        SuoYaoActivityProto.SuoYaoDrawBoxReqMsg reqMsg = SuoYaoActivityProto.SuoYaoDrawBoxReqMsg.parseFrom(packet.getBytes());
        drawIndex = reqMsg.getDrawIndex();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = SuoYaoMgr.getActivityInfo();
        if (!ActivityMgr.activityInShowTime(activityInfo)) {
            ret = GameErrorCode.E_SUOYAO_ACTIVITY_NO_OPEN;
            return;
        }
        SuoYaoModule module = player.getModule(SuoYaoModule.class);
        if (module.getUserData() == null) {
            ret = GameErrorCode.E_SUOYAO_ACTIVITY_NO_UNLOCK;
            return;
        }
        ret = module.drawBox(drawIndex);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            SuoYaoActivityProto.SuoYaoDrawBoxRespMsg.Builder builder = SuoYaoActivityProto.SuoYaoDrawBoxRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_SUOYAO_ACTIVITY_DRAW_BOX, builder);
        }
    }
}
